//
//  YCCharExchangeCryption.m
//  YCCryption
//
//  Created by 唐可雄 on 2017/7/31.
//  Copyright © 2017年 swustyc. All rights reserved.
//

#import "YCCharExchangeCryption.h"

@interface YCCharExchangeCryption ()

/**
 算法字典
 */
@property (strong, nonatomic) NSMutableDictionary *dic;

@end

@implementation YCCharExchangeCryption

#pragma mark - Getter and Setter

- (NSMutableDictionary *)dic {
    if (_dic == nil) {
        _dic = [[NSMutableDictionary alloc] init];
        [_dic setValue:@"A" forKey:@"u"];
        [_dic setValue:@"u" forKey:@"A"];
        [_dic setValue:@"V" forKey:@"B"];
        [_dic setValue:@"U" forKey:@"C"];
        [_dic setValue:@"o" forKey:@"D"];
        [_dic setValue:@"X" forKey:@"E"];
        [_dic setValue:@"c" forKey:@"F"];
        [_dic setValue:@"3" forKey:@"G"];
        [_dic setValue:@"p" forKey:@"H"];
        [_dic setValue:@"C" forKey:@"I"];
        [_dic setValue:@"n" forKey:@"J"];
        [_dic setValue:@"D" forKey:@"K"];
        [_dic setValue:@"F" forKey:@"L"];
        [_dic setValue:@"v" forKey:@"M"];
        [_dic setValue:@"b" forKey:@"N"];
        [_dic setValue:@"8" forKey:@"O"];
        [_dic setValue:@"l" forKey:@"P"];
        [_dic setValue:@"N" forKey:@"Q"];
        [_dic setValue:@"J" forKey:@"R"];
        [_dic setValue:@"j" forKey:@"S"];
        [_dic setValue:@"9" forKey:@"T"];
        [_dic setValue:@"Z" forKey:@"U"];
        [_dic setValue:@"H" forKey:@"V"];
        [_dic setValue:@"E" forKey:@"W"];
        [_dic setValue:@"i" forKey:@"X"];
        [_dic setValue:@"a" forKey:@"Y"];
        [_dic setValue:@"7" forKey:@"Z"];
        [_dic setValue:@"Q" forKey:@"a"];
        [_dic setValue:@"Y" forKey:@"b"];
        [_dic setValue:@"r" forKey:@"c"];
        [_dic setValue:@"f" forKey:@"d"];
        [_dic setValue:@"S" forKey:@"e"];
        [_dic setValue:@"m" forKey:@"f"];
        [_dic setValue:@"R" forKey:@"g"];
        [_dic setValue:@"O" forKey:@"h"];
        [_dic setValue:@"k" forKey:@"i"];
        [_dic setValue:@"G" forKey:@"j"];
        [_dic setValue:@"K" forKey:@"k"];
        [_dic setValue:@"A" forKey:@"l"];
        [_dic setValue:@"0" forKey:@"m"];
        [_dic setValue:@"e" forKey:@"n"];
        [_dic setValue:@"h" forKey:@"o"];
        [_dic setValue:@"I" forKey:@"p"];
        [_dic setValue:@"d" forKey:@"q"];
        [_dic setValue:@"t" forKey:@"r"];
        [_dic setValue:@"z" forKey:@"s"];
        [_dic setValue:@"B" forKey:@"t"];
        [_dic setValue:@"6" forKey:@"u"];
        [_dic setValue:@"4" forKey:@"v"];
        [_dic setValue:@"M" forKey:@"w"];
        [_dic setValue:@"q" forKey:@"x"];
        [_dic setValue:@"2" forKey:@"y"];
        [_dic setValue:@"g" forKey:@"z"];
        [_dic setValue:@"P" forKey:@"0"];
        [_dic setValue:@"5" forKey:@"1"];
        [_dic setValue:@"s" forKey:@"2"];
        [_dic setValue:@"y" forKey:@"3"];
        [_dic setValue:@"T" forKey:@"4"];
        [_dic setValue:@"L" forKey:@"5"];
        [_dic setValue:@"1" forKey:@"6"];
        [_dic setValue:@"w" forKey:@"7"];
        [_dic setValue:@"W" forKey:@"8"];
        [_dic setValue:@"x" forKey:@"9"];
        [_dic setValue:@"+" forKey:@"+"];
        [_dic setValue:@"/" forKey:@"/"];
    }
    
    return _dic;
}

#pragma mark - Cryption

- (NSString *)encrypt:(NSString *)str {
    NSMutableString *destStr = [[NSMutableString alloc] init];
    
    NSInteger length = str.length;
    for (int i = 0; i < length; i++) {
        NSString *strChar = [str substringWithRange:NSMakeRange(i, 1)];
        if (self.dic[strChar]) {
            [destStr appendString:self.dic[strChar]];
        } else {
            [destStr appendString:strChar];
        }
    }
    
    return destStr;
}

- (NSString *)decrypt:(NSString *)str {
    NSMutableString *destStr = [[NSMutableString alloc] init];
    
    NSArray *keys = self.dic.allKeys;
    NSArray *values = self.dic.allValues;
    NSInteger length = str.length;
    for (int i = 0; i < length; i++) {
        NSString *strChar = [str substringWithRange:NSMakeRange(i, 1)];
        if ([values containsObject:strChar]) {
            [destStr appendString:keys[[values indexOfObject:strChar]]];
        } else {
            [destStr appendString:strChar];
        }
    }
    
    return destStr;
}

@end
